package com.cortex.pineapple.adapters;

import java.util.ArrayList;
import java.util.List;

import com.cortex.pineapple.R;
import com.cortex.pineapple.view.CortexTextView;
import com.cortext.pineapple.link.ImageLink;
import com.cortext.pineapple.link.Link;
import com.cortext.pineapple.link.Link.OnDataChangedListener;
import com.cortext.pineapple.link.WebLink;

import android.content.Context;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class WebAdapter extends LinkAdapter<WebLink> {

	public WebAdapter(Context context) {
		super(context);
		links = new ArrayList<WebLink>();
	}
	public WebAdapter(Context context, List<WebLink> links) {
		super(context,links);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final WebLink link = links.get(position);
		ViewGroup linkView = (ViewGroup) convertView; 
		if(linkView == null){
			LayoutInflater inflater = LayoutInflater.from(context);
			linkView = (ViewGroup) inflater.inflate(R.layout.view_web_link, null);
			final ViewGroup finalLinkView = linkView;
			
			link.setOnDataChangedListener(new OnDataChangedListener() {
				
				@Override
				public void dataChanged(Link link) {
					buildItemView((WebLink) link, finalLinkView);
				}
			});
		}
		buildItemView(link, linkView);
		
		return linkView;
	}
	@Override
	protected void buildItemView(WebLink link, ViewGroup linkView) {
		CortexTextView titleText = (CortexTextView) linkView.findViewById(R.id.title);
		ImageView icon = (ImageView)linkView.findViewById(R.id.icon);
		
		titleText.setTextWithUnderLine(link.getTitle());
		((TextView)linkView.findViewById(R.id.content)).setText(link.getDescription());
		((TextView)linkView.findViewById(R.id.url)).setText(link.getUrl());
		
		if(link.getIcon() != null){
			icon.setVisibility(View.VISIBLE);
			icon.setImageBitmap(link.getIcon());
		}		

	}
}
